import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class TestProgram {
	public static void main(String[] args) throws IOException {
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		//String emailpattern = "[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*.[a-zA-Z]{2,3}";
		String line = "";
		String filepath="C:\\a";
		while (true) {
			StringBuffer result = new StringBuffer();
			String filename="";
			//start
			System.out.println("Welcome");
			
			//From : sender@comtrue.com
			line = br.readLine();
			if (line.equalsIgnoreCase("exit")) {
				break;
			}else if (!line.toLowerCase().startsWith("from")) {
				System.out.println("error");
				continue;
			}
			result.append(line + "\n");
			System.out.println("Sender ok");
			
			//To : receiver@comtrue.com
			line = br.readLine();
			if (!line.toLowerCase().startsWith("to")) {
				System.out.println("error");
				continue;
			}
			result.append(line + "\n");
			System.out.println("Recipient ok");
			
			//subject: test mail
			line = br.readLine();
			if (!line.toLowerCase().startsWith("subject")) {
				System.out.println("error");
				continue;
			}
			filename = line.split(":")[1];
			result.append(line + "\n");
			System.out.println("Subject ok");
			
			//data?????
			line = br.readLine();
			if (!line.toLowerCase().startsWith("data")) {
				System.out.println("error");
				continue;
			}
			//mail desc
			System.out.println("Enter mail, end with \".\"");
			do {
				line = br.readLine();
				result.append(line + "\n");
			} while (!line.equalsIgnoreCase("."));
			System.out.println("Message save");
			
			//result print
			System.out.println("\n\n"+result.toString()+"\n\n");
			
			File file = new File(filepath,filename);
			FileWriter fw = new FileWriter(file);
			fw.write(result.toString());
			fw.close();
		}
		
		br.close();
		isr.close();
		System.out.println("bye");
	}
}
